package com.ruoyi.web.controller.applets;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.applets.AppletsUtils;
import com.ruoyi.system.service.ISysConfigService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 小程序控制类
 *
 * @author KevenPotter
 * @date 2024-04-26 10:16:35
 */
@RestController
@RequestMapping("/applets/control")
@AllArgsConstructor
public class AppletsController {

    private final ISysConfigService sysConfigService;

    /**
     * 刷新小程序的AccessToken
     *
     * @return 返回小程序AccessToken
     * @author KevenPotter
     * @date 2024-04-26 15:46:39
     */
    @Anonymous
    @ResponseBody
    @GetMapping(value = "/refreshAccessToken")
    public AjaxResult refreshAccessToken() {
        String appletsAppId = sysConfigService.selectConfigByKey("applets_app_id");
        String appletsAppSecret = sysConfigService.selectConfigByKey("applets_app_secret");
        return AjaxResult.success(AppletsUtils.refreshAccessToken(appletsAppId, appletsAppSecret));
    }

    /**
     * 获取小程序AccessToken
     *
     * @return 返回小程序AccessToken
     * @author KevenPotter
     * @date 2024-04-26 15:46:47
     */
    @Anonymous
    @ResponseBody
    @GetMapping(value = "/getAccessToken")
    public AjaxResult getAccessToken() {
        return AjaxResult.success(AppletsUtils.getAccessToken());
    }
}
